package com.qf.j2205.controller;

import com.alibaba.fastjson.JSON;
import com.qf.j2205.entity.Role;
import com.qf.j2205.service.RoleService;
import com.qf.j2205.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @ClassName RoleServlet
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/17 20:00
 * @Version 1.0
 **/
public class RoleServlet extends ServletBase{

    /**
     * 查询全部数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService service = new RoleServiceImpl();
        List<Role> all = service.findAll();
        if (all.size() > 0){
            String json = JSON.toJSONString(all);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }

    }
}
